package cn.iocoder.yudao.module.dr.service.collectionlinkaddress;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress.vo.CollectionLinkAddressPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress.vo.CollectionLinkAddressSaveReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.report.vo.index.DeptUseDataRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.report.vo.index.FileTypeDataRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.report.vo.index.SourceTypeDataRespVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.collectionlinkaddress.CollectionLinkAddressDO;
import cn.iocoder.yudao.module.dr.dal.mysql.collectionlinkaddress.CollectionLinkAddressMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.COLLECTION_LINK_ADDRESS_NOT_EXISTS;

/**
 * 藏品模型链接地址 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectionLinkAddressServiceImpl implements CollectionLinkAddressService {

    @Resource
    private CollectionLinkAddressMapper collectionLinkAddressMapper;

    @Override
    public Long createCollectionLinkAddress(CollectionLinkAddressSaveReqVO createReqVO) {
        // 插入
        CollectionLinkAddressDO collectionLinkAddress = BeanUtils.toBean(createReqVO, CollectionLinkAddressDO.class);
        collectionLinkAddressMapper.insert(collectionLinkAddress);
        // 返回
        return collectionLinkAddress.getId();
    }

    @Override
    public void updateCollectionLinkAddress(CollectionLinkAddressSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectionLinkAddressExists(updateReqVO.getId());
        // 更新
        CollectionLinkAddressDO updateObj = BeanUtils.toBean(updateReqVO, CollectionLinkAddressDO.class);
        collectionLinkAddressMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollectionLinkAddress(Long id) {
        // 校验存在
        validateCollectionLinkAddressExists(id);
        // 删除
        collectionLinkAddressMapper.deleteById(id);
    }

    private void validateCollectionLinkAddressExists(Long id) {
        if (collectionLinkAddressMapper.selectById(id) == null) {
            throw exception(COLLECTION_LINK_ADDRESS_NOT_EXISTS);
        }
    }

    @Override
    public CollectionLinkAddressDO getCollectionLinkAddress(Long id) {
        return collectionLinkAddressMapper.selectById(id);
    }

    @Override
    public PageResult<CollectionLinkAddressDO> getCollectionLinkAddressPage(CollectionLinkAddressPageReqVO pageReqVO) {
        return collectionLinkAddressMapper.selectPage(pageReqVO);
    }

    /**
     * 查询列表
     * @Author 王铁
     * @Description TODO
     * @Date 2025/9/16 16:27
     * @Version 1.0
     */
    @Override
    public List<CollectionLinkAddressDO> getCollectionLinkAddressList(CollectionLinkAddressPageReqVO pageReqVO) {
        return collectionLinkAddressMapper.selectList(pageReqVO);
    }

    @Override
    public List<SourceTypeDataRespVO> countByImage() {
        return collectionLinkAddressMapper.countByImage();
    }

    @Override
    public List<FileTypeDataRespVO> countBy3d() {
        return collectionLinkAddressMapper.countBy3d();
    }

    @Override
    public List<DeptUseDataRespVO> countByBizType() {
        return collectionLinkAddressMapper.countByBizType();
    }

    @Override
    public List<CollectionLinkAddressDO> getCover(Long id, String bizType, String type) {
        LambdaQueryWrapper<CollectionLinkAddressDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectionLinkAddressDO::getCollectionId, id);
        wrapper.eq(CollectionLinkAddressDO::getBizType, bizType);
        wrapper.eq(CollectionLinkAddressDO::getType, type);
        return collectionLinkAddressMapper.selectList(wrapper);
    }

}